var util = require('../../utils/util.js')
Page({
  data: {
    list: [

    ],
    imghost: wx.getStorageSync("imghost"),
    sort: ['按距离排序', '按月销量排序', '按星级排序'],
    sorting: 0,

    distance: 2,
    istrue: false,
    sdis: ['1', '2', '4'], 
    sdis_in: 0,
    soldnum: ['100', '500'],
    soldnum_index: -1
  },
  onLoad: function() {
    this.getShopList();
  },
  getShopList: function() {
    var that = this;
    wx.getLocation({
      success: function(res) {
        var url = util.url.getsholist;
        var data = {
          x: res.longitude,
          y: res.latitude,
          sorting: that.data.sorting
        }
        if (that.data.soldnum_index>=0){
          data.soldnum = that.data.soldnum[that.data.soldnum_index]
        }
        if (that.data.sdis_in>=0){
          data.distance = that.data.sdis[that.data.sdis_in]
        }
        util.fetchPost(
          url,
          data,
          function(ret) {
            var list = ret.data;
            for (var i = 0; i < list.length; i++) {
              if (list[i].dis < 1) {
                list[i].dis = (list[i].dis * 1000).toFixed(0) + 'm';
              } else {
                list[i].dis = (list[i].dis).toFixed(2) + 'km';
              }
            }
            that.setData({
              list: ret.data
            })
          }
        )
      },
    })
  },
  goShop: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/index?id=' + id,
    })
  },
  bindSortChange: function(e) {
    var sorting = e.detail.value;
    this.setData({
      sorting: sorting
    })
    this.getShopList();
  },
  openDialog: function() {
    this.setData({
      istrue: true
    })
  },
  closeDialog: function() {
    this.setData({
      istrue: false
    })
  },
  setDistance: function(e) {
    var index = e.currentTarget.dataset.id;
    this.setData({
      sdis_in: index
    })
  },
  setSoldNum: function(e) {
    var index = e.currentTarget.dataset.id;
    this.setData({
      soldnum_index: index
    })
  },
  goToFilter:function(){
    this.getShopList();
    this.closeDialog();
  },
  shoucang:function(){
    console.log(1);
  }
})